from appium import webdriver
import datetime,os,time



import pymysql
#-----------------------------数据库信息-----------------
def DataUser():
    conn = pymysql.connect(host='128.1.135.199', port=3306, user='tac', passwd='8G6H4#D3', db='sug_colin')
    cur_User = conn.cursor()
    cur_User.execute("SELECT phone_number,uin FROM User ORDER BY create_at desc ")
    user_User = []
    for i in cur_User.fetchall():
        user_User.append(i)

    cur_Contacts = conn.cursor()
    cur_Contacts.execute("select uin2 From Contacts where uin = '100154'")
    user_Contacts = []
    for i in cur_Contacts.fetchall():
        user_Contacts.append(i)

    user_Contacts_new = []
    for i in user_Contacts:
        user_Contacts_new.append(i[0])

    phone_number = []
    for i in user_User:
        if not set(i) & set(user_Contacts_new):
            phone_number.append(i[0])

    cur_User.close()
    cur_Contacts.close()
    conn.close()
    return phone_number
#-----------------------------数据库信息-----------------

#-----------------------------包名相关信息-----------------
desired_caps = {'platformName': 'Android',
                 'deviceName': '127.0.0.1:7555',
                #'deviceName': 'VBJDU18C12000377',
                'platformVersion': '8.0',
                'appPackage': 'com.ssf.im',
                'appActivity': 'com.ssf.imkotlin.ui.welcome.WelcomeActivity'
                }
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
driver.implicitly_wait(5)
print('start App')

#-----------------------------------------------------------

#---------------------------定义函数------------------------
# 函数isElement，作用：判断页面元素是否存在，如果页面存在元素xx，则进行操作，不存在进行下一步
def isElement(id):
    try:
        driver.find_element_by_id(id)
        stateOne = True
    except:
        stateOne = False
    if stateOne == True:
        driver.find_element_by_id(id).click()
#函数swipeToUp，作用：滑动，向上滑动
def swipeToUp(during):
    width = driver.get_window_size()['width']
    height = driver.get_window_size()['height']
    driver.swipe(width / 2, height * 1 / 2, width / 2, height / 4, during)

#函数swipeTodown，作用：滑动，向下滑动
def swipeToDown(during):
    width = driver.get_window_size()['width']
    height = driver.get_window_size()['height']
    driver.swipe(width / 2, height / 4, width / 2, height * 3 / 4, during)

#-----------------------------------------------------------


for number in DataUser():
    #判断广告是否可以跳过
    try:
        driver.find_element_by_id("com.ssf.im:id/tv_count").click()
    except:
        pass

    phone_number = number
    #判断是否已经登录
    try:
        driver.find_element_by_xpath("//*[@resource-id='com.ssf.im:id/tv_title'and @text='我']").click()
        stateTwo = True
    except:
        stateTwo = False
    print('如果已经登录，退出重新登录')
    # stateTwo 为真，说明已经登录，退出登录
    if stateTwo == True:
        driver.find_element_by_id("com.ssf.im:id/ll_setting").click()
        driver.find_element_by_id("com.ssf.im:id/btn_logout").click()
        driver.find_element_by_id("com.ssf.im:id/dialog_tv_ensure").click()

    #登录
    driver.find_element_by_id("com.ssf.im:id/et_pwd").send_keys("13333333333")
    driver.find_element_by_id("com.ssf.im:id/etMessage").send_keys("0000")
    driver.find_element_by_id("com.ssf.im:id/tvLogin").click()
    # 判断是否有弹框
    isElement("com.ssf.im:id/tv_got_it")

    # 加好友
    driver.find_element_by_id("com.ssf.im:id/ll_right_bar").click()
    driver.find_element_by_id("com.ssf.im:id/tv_add_person").click()
    #搜索用户
    print(phone_number)
    driver.find_element_by_id("com.ssf.im:id/et_search").send_keys(phone_number)
    time.sleep(3)
    driver.find_element_by_id("com.ssf.im:id/iv_right_text").click()

    #判断用户是否存在
    try:
        driver.find_element_by_id("com.ssf.im:id/iv_right_text").click()
        driver.find_element_by_id("com.ssf.im:id/ll_left_bar").click()
        print('用户不存在')
        continue
    except:
        pass
    #打招呼
    swipeToUp(1000)
    #判断是否已经申请
    try:
        driver.find_element_by_name("已打招呼")
        stateTwo_A = False
    except:
        stateTwo_A = True
    #判断是否已经是好友
    try:
        driver.find_element_by_name("打招呼").click()
        stateTwo = True
    except:
        stateTwo = False
    print('如果已经是好友，重新搜索下一个')
    # stateTwo 为真，说明已经是好友，重新搜索下一个
    if stateTwo and stateTwo_A:
        driver.find_element_by_id("com.ssf.im:id/iv_back").click()
        driver.find_element_by_id("com.ssf.im:id/ll_left_bar").click()
        continue

    driver.find_element_by_name("Hi，有空吗，聊聊呗").click()
    driver.find_element_by_id("com.ssf.im:id/iv_back").click()
    driver.find_element_by_id("com.ssf.im:id/ll_left_bar").click()
    #我-退出按钮
    #driver.find_element_by_xpath('//android.widget.TextView[contains(@text, "我")]').click()
    driver.find_element_by_xpath("//*[@resource-id='com.ssf.im:id/tv_title'and @text='我']").click()
    driver.find_element_by_id("com.ssf.im:id/ll_setting").click()
    driver.find_element_by_id("com.ssf.im:id/btn_logout").click()
    driver.find_element_by_id("com.ssf.im:id/dialog_tv_ensure").click()
    #登录好友账号
    driver.find_element_by_id("com.ssf.im:id/et_pwd").send_keys(phone_number)
    driver.find_element_by_id("com.ssf.im:id/etMessage").send_keys("0000")
    driver.find_element_by_id("com.ssf.im:id/tvLogin").click()

    # 判断是否已经注册完成
    try:
        driver.find_element_by_name("Hi，有空吗，聊聊呗").click()
        driver.find_element_by_id("com.ssf.im:id/tv_send").click()
        driver.find_element_by_id("com.ssf.im:id/tv_got_it").click()
    except:
        pass

    #好友申请通过
    #driver.find_element_by_id("com.ssf.im:id/surfaceView").click()
    #处理好友申请信息
    isElement("com.ssf.im:id/tv_greet")
    driver.find_element_by_id("com.ssf.im:id/ll_left_bar").click()
    driver.find_element_by_id("com.ssf.im:id/ll_left_bar").click()
    #我-退出按钮
    driver.find_element_by_xpath("//*[@resource-id='com.ssf.im:id/tv_title'and @text='我']").click()
    driver.find_element_by_id("com.ssf.im:id/ll_setting").click()
    driver.find_element_by_id("com.ssf.im:id/btn_logout").click()
    driver.find_element_by_id("com.ssf.im:id/dialog_tv_ensure").click()